#include "Protocol.hpp"
#include"Sock.hpp"
#include<pthread.h>
static void Usage(string proc)
{
    std::cout<<"Usage: "<<proc<<"proc"<<endl;
    exit(1);
}
void* HandlerRequest(void *args)
{
    int sock=*(int *)args;
    delete (int*)args;
    pthread_detach(pthread_self());
    //业务逻辑,做一个短服务，
    //request->分析处理->构建response->sent(response)->close(sock)
    //version1.原生方法，没有明显的序列化和反序列化的过程
    //1.读取请求
    char buffer[1024];
    ssize_t s=read(sock,buffer,sizeof(buffer)-1);
    request_t req;
    if(s>0)
    {
        buffer[s]=0;
        cout<<"get a new request:"<<buffer<<endl;
        std::string str=buffer;
    
        DeserializeRequest(str,req);
       
    }
    // request_t req;
    // ssize_t s=read(sock,&req,sizeof(req));
    if(s==sizeof(req))
    {
        //读取到了完整的请求，待定
        response_t resp{0,0};
        //2.分析请求 && 3.计算结果
        //4.构建响应，并进行返回
        switch(req.op)
        {
            case '+':
                resp.result=req.x+req.y;
                break;
            case '-':
                resp.result=req.x-req.y;
                break;
            case '*':
                resp.result=req.x*req.y;
                break;
            case '/':
                if(req.y==0)resp.code=-1;//代表除0
                else resp.result=req.x/req.y;
                break;
            case '%':
                if(req.y==0) resp.code==-2;//代表模0
                else resp.result=req.x%req.y;
                break;
            default:
                resp.code=-3;//代表请求方法异常
                break;
        }
        //write(sock,&resp,sizeof(resp));
        std::string send_string =SerializeResponse(resp);
        write(sock,send_string.c_str(),send_string.size());
        cout<<"服务结束"<<send_string<<endl;
    }
    //5.关闭连接
    // cout<<req.x<<req.op<<req.y<<endl;
    close(sock);
}
int main(int argc,char* argv[])
{
    if(argc!=2)Usage(argv[0]);
    uint16_t port=atoi(argv[1]);

    int listen_sock=Sock::Socket();
    Sock::Bind(listen_sock,port);
    Sock::Listen(listen_sock);

    for( ; ; )
    {
        int sock=Sock::Accept(listen_sock);
        if(sock>=0)
        {
            cout<<"get a new client..."<<endl;
            int *pram=new int(sock);
            pthread_t tid;
            pthread_create(&tid,nullptr,HandlerRequest,pram);
        }
      
    }
    return 0;
}